package adavance.socket;

import java.io.*;
import java.net.Socket;

/**
 * @author oldliu
 * @since 1.0
 */
public class FileSendThread2 extends Thread {
    private final Socket s;

    public FileSendThread2(Socket s) {
        this.s = s;
    }

    public static final String FOLDER = "D:\\Documents\\图片和资料";

    @Override
    public void run() {
        InputStream in = null;
        OutputStream out = null;
        try {
            in = s.getInputStream();
            out = s.getOutputStream();
            //PrintWriter pw = new PrintWriter(out,true);
            //1:读一个文件名
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String filename = br.readLine();
            String filelocation = FOLDER + "\\" + filename;//获得文件的具体位置
            File file = new File(filelocation);
            if (!file.exists()) {
                out.write(0);
                out.flush();
            } else if (file.isDirectory()||file.canRead()==false) {
                out.write(0);
                out.flush();
            } else {
                out.write(1);
                out.flush();
                FileInputStream fin = new FileInputStream(filelocation);
                byte[] b = new byte[fin.available()];
                int len = fin.read(b);
                out.write(b, 0, len);
                out.flush();
                System.out.println("文件发送成功！");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                s.close();
            } catch (Exception e) {

            }
        }

    }
}
